//
//  KAAlertVC.swift
//  kano
//
//  Created by Sophia on 2024/8/21.
//

import UIKit

class KAAlertVC: UIViewController {
    
    lazy var var_arrowImg: UIImageView = {
        let var_arrowImg = UIImageView(image: UIImage(named: ""))
        return var_arrowImg
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.text = ""
        var_titleLabel.textColor = .black
        var_titleLabel.font = kBoldFontSize(20)
        var_titleLabel.numberOfLines = 0
        var_titleLabel.textAlignment = .center
        var_titleLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_titleLabel
    }()
    
    lazy var var_messageLabel: UILabel = {
        let var_messageLabel = UILabel()
        var_messageLabel.text = ""
        var_messageLabel.textColor = .black
        var_messageLabel.font = kFontSize(15)
        var_messageLabel.textAlignment = .center
        var_messageLabel.numberOfLines = 0
        var_messageLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_messageLabel
    }()
    
    lazy var var_signBtn: UIButton = {
        let var_signBtn = UIButton()
        var_signBtn.setTitle("=", for: .normal)
        var_signBtn.setTitleColor(.themeColor, for: .normal)
        var_signBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(17), weight: .bold)
        var_signBtn.backgroundColor = .colorE7E7FB
        var_signBtn.layer.cornerRadius = kscale(25)
        var_signBtn.layer.masksToBounds = true
        var_signBtn.addTarget(self, action: #selector(ka_buttonClick), for: .touchUpInside)
        return var_signBtn
    }()
    
    @objc func ka_buttonClick() {
        guard handler != nil else {
            return
        }
        handler!()
    }
    var handler:(()->())? = nil
    
}

extension KAAlertVC {
    
    convenience init(title: String, subTitle: String, actionTitle: String, image: String) {
        self.init(nibName: nil, bundle: nil)
        var_titleLabel.text = title
        var_messageLabel.text = subTitle
        var_signBtn.setTitle(actionTitle, for: .normal)
        var_arrowImg.image = UIImage(named: image)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(var_arrowImg)
        view.addSubview(var_titleLabel)
        view.addSubview(var_messageLabel)
        view.addSubview(var_signBtn)
        
        var_arrowImg.snp.makeConstraints { make in
            make.width.height.equalTo(kscale(kscale(60)))
            make.centerX.equalTo(self.view)
            make.top.equalTo(kscale(24))
        }
        
        var_titleLabel.snp.makeConstraints { make in
            make.left.equalTo(view).offset(kscale(44))
            make.right.equalTo(view).offset(kscale(-44))
            make.top.equalTo(var_arrowImg.snp.bottom).offset(kscale(12))
        }
        var_messageLabel.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.top.equalTo(var_titleLabel.snp.bottom).offset(kscale(12))
        }
        var_signBtn.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.height.equalTo(kscale(50))
            make.top.equalTo(var_messageLabel.snp.bottom).offset(kscale(32))
            make.bottom.equalTo(view).offset(kscale(-20))
        }
        
        if let gestureRecognizers = self.view.gestureRecognizers {
            for recognizer in gestureRecognizers {
                if recognizer is UIPanGestureRecognizer {
                    self.view.removeGestureRecognizer(recognizer)
                }
            }
        }
        
    }
}
